# -*- coding: utf-8 -*-
"""
@Time ： 2023/9/13 8:20
@Auth ： y.h
@File ：object_list_conversion.py
@IDE ：PyCharm
@Motto：The sooner you start to code, the longer the program will take. —— Roy Carlson
"""

from pythonProjectTemplate.Entity.DataEntity.template_entity import template_entity
from pythonProjectTemplate.Entity.DataEntity.BOM_entity import bom_entity
from pythonProjectTemplate.Entity.DataEntity.cost_entity import cost_entity
from pythonProjectTemplate.Entity.DataEntity.price_entity import price_entity
from pythonProjectTemplate.Entity.SystemEntity.entity_load import entity_load, get_entity_setFunc

__all__ = ["object_to_list", "list_to_object"]


def object_to_list(obj: type) -> list:
    member = list(vars(obj))
    member_dict = vars(obj)
    to_list = []
    arg_list = []
    for a in range(len(member)):
        # 筛掉python类内置元素

        if member[a][:3] == "get":
            arg_list.append(member[a])
    for m_fun in arg_list:
        if m_fun in member_dict.keys():
            f = member_dict.get(m_fun)(obj)
            to_list.append(f)
    return to_list


def list_to_object(data_list: list, titles_dict: dict, sheet_name: str,
                   primary_key: int) -> dict:
    """
    列表转对象字典
    :param primary_key:
    :param titles_dict:
    :param entity:
    :param sheet_name:
    :type sheet_name:
    :param data_list: 传入的数据列表对象
    :type data_list: list[list[str|int]]
    :return: 对象字典
    :rtype: dict
    """

    # 自动装载实体类 开始
    if sheet_name in titles_dict.keys():
        print(sheet_name)
        entity = entity_load(titles_dict.get(sheet_name), sheet_name)
        set_func_list = get_entity_setFunc(entity)
        entity_dict = dict()
        if len(set_func_list) == len(data_list[0]):
            increment_primary_key = 0
            for data_list_one in data_list:
                increment_primary_key += 1
                if sheet_name in titles_dict.keys():
                    entity = entity_load(titles_dict.get(sheet_name), sheet_name)
                    for func, data in zip(set_func_list, data_list_one):

                        set_func = getattr(entity, func)
                        set_func(entity, data)
                    if primary_key == "increment":
                        entity_dict.update({increment_primary_key: entity})
                    else:
                        entity_dict.update({data_list_one[primary_key]: entity})
        return entity_dict
        # 自动装载实体类 结束
